package com.example.myapplication.service;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;

import com.example.myapplication.R;

public class ServiceActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service);
    }

    public void startService(View view) {
        startService(new Intent(this,MyService.class));
    }

    public void stopService(View view) {
        stopService(new Intent(this,MyService.class));
    }

    public void bindService(View view) {
        bindService(new Intent(this,MyService.class),connecton, Context.BIND_AUTO_CREATE);
    }

    public void unBindService(View view) {
        unbindService(connecton);
    }

    private ServiceConnection connecton = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e("onServiceConnected","连接服务成功");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e("onServiceConnected","连接服务失败");
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connecton);
    }
}